package com.example.demo;

import com.example.demo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张杰
 * Date: 2022-08-04
 * Time: 15:29
 */

@Slf4j //加载日志
//默认情况下是要返回静态页面的,ResponseBody的作用就是表明这是一个非静态页面的数据
@ResponseBody
@Controller
@RequestMapping("/user")  //类上面的RequestMapping省略
public class UserController {

    //从配置文件中读取图片的保存路径
    @Value("${img.path}")
    private String imgPath;

    @RequestMapping("/login2")
    public HashMap<String,Object> lodin2(String username,String password) {
        HashMap<String,Object> result = new HashMap<String,Object>();
        int state = 200;
        int data = -1; //等于1，表示登录成功
        String msg = "";
        if(StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
            if(username.equals("admin") && password.equals("admin")) {
                data = 1;
                msg="";
            }else {
                msg = "用户名或密码失败！";
            }
        }else { //参数为空
            msg = "非法参数";
        }
        result.put("state",state);
        result.put("data",data);
        result.put("msg",msg);
        return result;
    }

    //获取全部的Cookie
    @RequestMapping("/cookie")
    public void getCookie(HttpServletRequest request) {
        //得到全部 Cookie
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies) {
            log.info("Cookie key:" + cookie.getName() +
                    " | Cookie Value: " + cookie.getValue());
        }
    }

    //获取某一个Cookie
    @RequestMapping("/cookie2")
    public String getCookie2(@CookieValue("bite") String cookie) {
        return "Cookie Value: " + cookie;
    }

    @RequestMapping("/sayhi")  //方法上的RequestMapping一定不能省略
    public String sayHi() {
        return "你好哇,边伯贤" + " "+imgPath;
    }

    @RequestMapping(method = RequestMethod.POST,value = "/sayhi2")
    public String sayHi2() {
        return "Hello";
    }

    @PostMapping("/sayhi3")
    public String sayHi3() {
        return "你好,朴灿烈";
    }

    @GetMapping("/sayhi4")
    public String sayHi4() {
        return "你好,朴灿烈";
    }

    @RequestMapping("/getuserbyid")
    public UserInfo getUserById (Integer id) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setUsername("张三");
        userInfo.setAge(18);
        return userInfo;
    }

    @RequestMapping("/login")
    public String login(String username,String password) {
       return "用户名:" + username + " | 密码: " + password;
    }

    //前端传递对象
    @RequestMapping("/reg")
    public String reg(@RequestBody UserInfo userInfo) {
        return "用户信息:" + userInfo;
    }

    //从URL的地址部分获取参数
    @RequestMapping("/hero/{id}/{name}")
    public String getHeroId(@PathVariable Integer id,@PathVariable String name) {
        return "ID:" + id + " " + "name:" + name;
    }

    @RequestMapping("")
    public String getSession (@SessionAttribute(value = "userinfo",
           required = false) String userinfo) {
        return "会话：" + userinfo;
    }

    //获取文件
    @RequestMapping("/upimg")
    public boolean upImg(Integer uid, @RequestPart("img") MultipartFile file) {
        boolean result = false;
        //保存图片到本地目录 = imgpath
        //图片名称(图片名称不能重复) [时间戳/UUID]
        //获取原上传图片的格式
        String fileName = file.getOriginalFilename();//得到图片的名称(xxx.png)
        fileName = fileName.substring(fileName.lastIndexOf("."));// 得到原图片的后缀(png)
        fileName = UUID.randomUUID().toString() + fileName;


        try {
            file.transferTo(new File(imgPath + fileName));
            result = true;
        } catch (IOException e) {
            log.error("上传图片失败: " + e.getMessage());
        }
        return result;
    }
}
